package cloud.maque.module.system.service.impl;

import cloud.maque.module.system.entity.SysUserRole;
import cloud.maque.module.system.mapper.UserRoleMapper;
import cloud.maque.module.system.service.UserRoleService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, SysUserRole> implements UserRoleService {

    @Override
    public List<SysUserRole> GetList(SysUserRole data) {

        QueryWrapper<SysUserRole> query = new QueryWrapper<>();
        if (!StrUtil.isEmptyIfStr(data.getRoleId())) {
            query.lambda().eq(SysUserRole::getRoleId, data.getRoleId());
        }
        if (!StrUtil.isEmptyIfStr(data.getUserId())) {
            query.lambda().like(SysUserRole::getUserId, data.getUserId());
        }
        return this.list(query);
    }

    @Override
    public SysUserRole SaveUserDept(SysUserRole data) {
        this.saveOrUpdate(data);
        return data;
    }

    @Override
    public Boolean DeleteByRoleId(String roleId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserRole::getRoleId, roleId);
        return this.remove(queryWrapper);
    }

    @Override
    public Boolean DeleteByUserId(String userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserRole::getUserId, userId);
        return this.remove(queryWrapper);
    }
}
